package cn.lanqiao.dao.impl;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.lanqiao.dao.ProjectDao;
import cn.lanqiao.entiy.Project;
import cn.lanqiao.util.JsfUtil;

public class ProjectDaoImpl implements ProjectDao {
	

	@Override
	public Object[][] listProjectData() {
        Connection conn =JsfUtil.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		String sql="select * from J_project";
		Object[][] datas=null;
		int j=0;
		try {
			ps=conn.prepareStatement(sql);
		    rs=ps.executeQuery();
		 
			datas=new Object[5][];
		    int i=0;
		    j=count();
		    datas=new Object[j][];
		   
		    
		    while(rs.next()) { 	
		   String id= rs.getString("P_ID");
		   String name=	 rs.getString("PNAME");
		   String price = rs.getString("PPRICE");
//           System.out.println(id  +  name +  price);
		   
             datas[i]=new Object[] {
		    			id,name,price
		    	};
		        ++i;
		    }
		 
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
		JsfUtil.close(rs, ps, conn);	
		}
        
        
		return datas;
	}

    public  int count() {
    	  Connection conn =JsfUtil.getConnection();
  		PreparedStatement ps=null;
  		ResultSet rs=null;
  		String sql="select * from J_project";  
  		int j=0;
  		try {
  		ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			
			 while(rs.next()) {
			    	j++;
			    }
	    	
  		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
  		finally {
			JsfUtil.close(rs, ps, conn);	
			}
    	
  		return j; 
    }
	public Project getdatas(Project project) {
	Project pdata=null;
	Connection conn=JsfUtil.getConnection();
	PreparedStatement ps=null;
	ResultSet rs =null;
	String sql="select * from J_project";
	try {
		ps= conn.prepareCall(sql);
	    rs=ps.executeQuery(); 
	    int i=0;
	    while(rs.next()) {
		  i++;
		  String id= rs.getString("P_ID");
		  String name=rs.getString("PNAME");
		  String price=rs.getString("PPRICE");
		
		  
		  //这个地方只能用equals比较属性值；==地址不一样不能找到相同name
	      if(project.getP_id().equals(id)) {
	    	 pdata= new Project(id, name, price);  
	         break;
	      }
	  }
	    System.out.println(i);
	} catch (SQLException e) {
		e.printStackTrace();
	}
	finally {
		JsfUtil.close(rs, ps, conn);
	}
	
	
	return pdata;
	}
    public int addProjectData(Project project) {
    Connection conn=JsfUtil.getConnection();
    PreparedStatement ps=null;
//    ResultSet rs=null;
    String sql="insert into J_project(p_id,pname,pprice)  values(?,?,?)";
    int after = 0;
    try {
		ps=conn.prepareStatement(sql);
//	    rs=ps.executeQuery();
	    //添加数据
	    ps.setString(1,project.getP_id());
        ps.setString(2,project.getPpname());
        ps.setString(3,project.getPprice());
         after=ps.executeUpdate();
//        if(after>0) {
//        	System.out.println("添加成功");
//        }else {
//        	System.out.println("添加失败");
//        }
       
    } catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally {
	 JsfUtil.close(ps, conn);
		 
	}
    return after;
    }
    
    public int deleteProjectData(Project project) {
    	Connection conn=JsfUtil.getConnection();
    	PreparedStatement ps=null;
    	String sql="delete from J_project where P_id=?";
        int after=0;
    	try {
			ps=conn.prepareStatement(sql);
			
		
				ps.setString(1,project.getP_id());
			
			
			 after=ps.executeUpdate();
			if(after>0) {
				System.out.println("删除成功");
			}else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JsfUtil.close(ps, conn);
		}
    	return after;
    }
    public int UpdateProjecttata(Project project) {
    	Connection conn=JsfUtil.getConnection();
    	PreparedStatement ps=null;
    	String sql="update J_project set pname=?,pprice=? where P_id=?";
        int after=0;
    	try {
			ps=conn.prepareStatement(sql);
			ps.setString(1,project.getPpname());
			ps.setString(2,project.getPprice());
			ps.setString(3,project.getP_id());
			
			 after=ps.executeUpdate();
			if(after>0) {
				System.out.println("更新成功");
			}else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JsfUtil.close(ps, conn);
		}
    	return after;
    }

}
